import json
import pinyin
import re
import requests
import time

from db import *
from settings import *
from spider_xue.new_ip import new_ip_obj
from spider_xue.sp import play_for_day_without_flag, play_for_five_without_flag, play_for_one_no_flag
from spider_xue.thread_file import mission_five, mission_day_all_stock, mission_fix_one_mintue


def getStrAllAplha(strs):
    return pinyin.get_initial(strs).upper()

def spiderStock(page, url):
    header = {}
    response = requests.get(url % page, header)
    reRes = re.findall(re.compile("[(](.*)[)]"), response.text)
    return json.loads(reRes[0])

def collect_stock():
    tmpList = []
    count = 0
    for i in range(1, 234):
        res = spiderStock(i, url)
        stockNameList = res["data"]["diff"]
        for name in stockNameList:
            tmpDict = {}
            stockName = name["f14"]
            tmpStr = ""
            if name["f2"] == '-':
                continue
            for strs in stockName:
                tmpStr += getStrAllAplha(strs)
            count += 1
            tmpDict["alpha"] = tmpStr
            tmpDict["name"] = name["f14"]
            tmpDict["code"] = name["f12"]
            tmpDict["price"] = name["f20"]
            if name["f13"]:
                tmpDict["prefix"] = "SH"
            else:
                tmpDict["prefix"] = "SZ"
            tmpDict["use"] = 0
            
            tmpDict["symbol"] = tmpDict['prefix'].upper() + tmpDict["code"]
            if '退' not in tmpDict["name"] and not str(tmpDict["code"]).startswith("3") and not str(tmpDict["code"]).startswith("688") and not str(tmpDict["name"]).startswith("C") and not str(tmpDict["name"]).startswith("N") and "ST" not in tmpDict["name"]:
                # 大于45亿市值 股价大于4
                if tmpDict["name"] not in lei and tmpDict["price"] != '-' and tmpDict["price"] > 1300000000:
                    if tmpDict["price"] <= 5000000000:
                        tmpDict["use"] = 1
                    elif 5000000000 < tmpDict["price"] <= 10000000000:
                        tmpDict["use"] = 2
                    elif 10000000000 < tmpDict["price"] <= 40000000000:
                        tmpDict["use"] = 3
                    elif 40000000000 < tmpDict["price"] <= 80000000000:
                        tmpDict["use"] = 4
                    else:
                        tmpDict["use"] = 5
            print(tmpDict["symbol"])

            stock_db.update_one({"symbol": tmpDict["symbol"]}, {
                             "$set": tmpDict}, upsert=True)
        time.sleep(1)
    return tmpList

def ths_res(code):
    ths_tcgn = ''
    try:
        ths_url1 = ths_url % code
        headers = {"user-agent": "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Mobile Safari/537.36 Edg/95.0.1020.44",
                   "cookie": "log=; spversion=20130314; reviewJump=nojump; searchGuide=sg; usersurvey=1; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1636527045,1636527397; historystock=601000%7C*%7C603906; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1636536683; hxmPid=free_stock_gainian_000609; v=A4Par2Jf5mHE-qqVNtOQh_SKEkwt-Bc6UYxbbrVg3-JZdKx2PcinimFc67zG"}
        ths_tcgn_res = requests.get(url=ths_url1, headers=headers)

        for jksd in ths_tcgn_res.json()['data']:
            if jksd['title'] in llo:
                continue
            ths_tcgn = ths_tcgn + ' ' + jksd['title']

        return ths_tcgn
    except Exception as e:
        print(e)
        return ths_tcgn

def fix_hxtc():
    mycol = stock_db.find()
    for sto in mycol:
        if not sto.get('hxtc'):
            print(sto["symbol"])
            searchUrl1 = infoUrl1 % sto["symbol"].lower()
            req = requests.get(searchUrl1)
            try:
                info = req.json()
            except:
                continue
            sto['gsjj'] = info["jbzl"]['gsjj'].strip()
            sto['jyfw'] = info["jbzl"]['jyfw'].strip()

            searchUrl2 = infoUrl2 % sto["symbol"].lower()
            req2 = requests.get(searchUrl2)
            try:
                info2 = req2.json()
            except:
                continue
            if not info2.get('hxtc'):
                continue
            sto['ydnr'] = info2['hxtc'][0]['ydnr'].strip()
            stock_db.update_one({"symbol": sto["symbol"]}, {
                                "$set": sto}, upsert=True)
            time.sleep(1)

        if not sto.get('ths_hxtc'):
            print(sto["symbol"])
            ths_tcgn = ths_res(sto["code"])
            sto['ths_hxtc'] = ths_tcgn.strip()

            stock_db.update_one({"symbol": sto["symbol"]}, {
                                "$set": sto}, upsert=True)
            time.sleep(1)

############################################################################################
def get_hot_ip_job(count):
    count_db = settings_db.find_one({"name": 'count'})
    if not count:
        count = count_db['end']
    end = count_db['end']
    if count >= end:
        end = new_ip_obj.get_req(count)
        settings_db.update_one({"count": 1}, {"$set": {'end': end}}, upsert=True)
    return end

#############################################################################################

def save_five_days_record(days=-20):
    st = data_day_db.find_one({'symbol': 'SZ002466'})
    ts = st['item'][days][0] / 1000
    all = record_main_db.find({"ts": {"$gte": 1637078400}}).sort([('ts', 1)])
    record_day_db.delete_many({"date": {"$gte": '1'}})
    ll = []
    for i in all:
        datte = time.strftime('%Y-%m-%d', time.localtime(i['ts']))
        record_day_db.update_one({"symbol": i['symbol'], 'date': datte}, {"$set": {"symbol": i['symbol'], 'date': datte}}, upsert=True)
        if i['symbol'] not in ll:
            print(i['symbol'])
            ll.append(i['symbol'])
    print(len(ll))

###############################################################################################

def set_yesterday_flag():
    ts = 1636300800
    all = record_db.find({"ts": {"$gte": ts, }}).sort([('ts', 1)])
    ll = {}
    for i in all:
        d = time.strftime('%Y-%m-%d', time.localtime(i['ts']))
        key = i['symbol'] + '?' + d
        if key not in ll:
            ll[key] = []
        ll[key].append(i)
    
    for k, v in ll.items():
        s, d = k.split('?')
        ts = int(time.mktime(time.strptime(d, '%Y-%m-%d')))
        
        if record_main_db.find_one({"symbol": s, 'ts': ts}):
            record_main_db.update_one({"symbol": s, 'ts': ts}, {"$push": {"sons": v}})
            
        else:
            record_main_db.update_one({"symbol": s, 'ts': ts}, 
            {"$set": {'ts': ts,'macd': i.get('macd', 0), 
            'high': i.get('high', 0), 'sons': v}}, upsert=True)

    print(len(ll))

def set_yesterday_flag1():
    ts = 1636300800
    all = record_yes_db.find({"ts": {"$gte": ts, }}).sort([('ts', 1)])
    record_day_db.delete_many({"date": {"$gte": "2021"}})
    ll = 0
    for i in all:
        ll += 1
        key = {"symbol": i['symbol'], 'date': time.strftime('%Y-%m-%d', time.localtime(i['ts']))}
        record_day_db.update_one(key, {"$set": key}, upsert=True)
    print(ll)

if __name__ == '__main__':
    ct = 5
    all = 1
    if ct == 0:
        collect_stock()
    elif ct == 1:
        fix_hxtc()
    elif ct == 2:
        get_hot_ip_job(0)
    elif ct == 3:
        play_for_one_no_flag(mission_fix_one_mintue)
    elif ct == 4:
        play_for_five_without_flag(mission_five, all)
    elif ct == 5:
        play_for_day_without_flag(mission_day_all_stock, all)
    elif ct == 6:
        save_five_days_record(-20)
    elif ct == 7:
        set_yesterday_flag1()
    else:
        g = east_record_db.distinct("date")
        g.sort()
        
        print(g)
        
    
